package com.yoyo.itravel.web.controller;

import com.yoyo.itravel.bean.*;
import com.yoyo.itravel.exceptions.RoleException;
import com.yoyo.itravel.exceptions.SightException;
import com.yoyo.itravel.service.IRoleService;
import com.yoyo.itravel.utils.JSONUtils;
import com.yoyo.itravel.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
*@Description:角色控制层
*@Author:CRR
*@Date：18:49 2020/6/22
*/
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    /**
     * @Description 分页查询所有景点
     * @Param page
     * @Param size
     * @Param response
     * @Return void
     */
    @RequestMapping("/findAllRoleByPage")
    @ResponseBody
    protected void findAllSRoleByPage(Integer page, @RequestParam("limit") Integer size, HttpServletResponse response) throws ServletException, IOException {
        // 参数合法性检测
        if (page == null || page < 1) {
            // 如果没有传起始页码，则默认为第一页
            page = 1;
        }
        if (size == null) {
            // 如果没有传递页码显示数据条数，则默认显示10条数据
            size = 10;
        }
        // 构建PageBean对象
        PageBean pageBean = new PageBean((page - 1) * size, size);
        // 构建JsonResult对象
        JsonResult jsonResult = new JsonResult();

        try {
            // 分页查询景点数据
            List<Role> roles = roleService.findAllByPage(pageBean);
            // 查询景点数据的总条数
            Integer totalCount = roleService.findTotalCount();
            // 将景点数据和总条数设置到JsonResult对象中
            jsonResult.setData(roles);
            jsonResult.setCount(totalCount);
            JSONUtils.writeValueToResponse(jsonResult, response);
        } catch (RoleException e) {
            e.printStackTrace();
        }
    }





    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 通过角色id删除角色
     */
    @RequestMapping("/deleteRoleByRoleId")
    protected void deleteByRoleId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得传过来的数据
        String roleId = request.getParameter("roleId");
        System.out.println(roleId);

        try {
             roleService.deleteByRoleId(roleId);

        } catch (RoleException e) {
            e.printStackTrace();
        }
    }

    /**
     * @throws ServletException
     * @throws IOException
     * @Description 添加角色
     */
    @RequestMapping("/addRole")
    protected void addRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleName = request.getParameter("roleName");
        String roleDesc = request.getParameter("roleDesc");

        Role role = new Role();
        role.setRoleName(roleName);
        role.setDescription(roleDesc);
        System.out.println(role);
        try {
            roleService.addRole(role);
        } catch (RoleException e) {
            e.printStackTrace();
        }
    }


}
